﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

using WpfApp4.Models.Config;

namespace WpfApp4.Utilities
{
    public static class ConfigManager
    {
        public static MenuInfo LoadConfig()
        {
            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "menu.json");
            string ret = JsonHelper.ReadJson(fileName, out MenuInfo menus, null);
            if (!string.IsNullOrEmpty(ret) || menus == null)
            {
                return null;
            }
            return menus;
        }

        public static void SaveConfig()
        {
            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "menu.json");
            var data = MenuInfo.Instance;
            JsonHelper.SaveToJson(ref data, fileName);

        }

        public static List<MenuItem> CreateMenuItem(MenuConfig config)
        {
            List<MenuItem> list = new List<MenuItem>();

            MenuItem menu = new MenuItem();
            Binding binding = new Binding(config.Command);

            menu.Header = config.Name;
            menu.SetBinding(MenuItem.CommandProperty, binding);
            menu.CommandParameter = config;

            if (config.MenuItems != null && config.MenuItems.Length > 0)
            {
                foreach(var item in config.MenuItems)
                {
                    var itemList = CreateMenuItem(item);
                    foreach(var item2 in itemList)
                    {
                        menu.Items.Add(item2);
                    }
                }
            }

            list.Add(menu);

            return list;
        }
    }
}
